<?php
require_once 'simple_html_dom.php';
/**
 *
 * Enter description here ...
 * @author Vladimir
 *
 */
class AdvegoParse
{
    private $_host;

    /**
     * @return the $_host
     */
    public function getHost()
    {
        return $this->_host;
    }

    /**
     * @param field_type $host
     */
    public function setHost( $host )
    {
        $this->_host = $host;
    }

    public function __construct( $host )
    {
        $this->setHost( $host );
    }

    public function getLinksListPageByUrl( $url )
    {
        Zend_Registry::get( 'logger' )->debug( 'getLinksListPageByUrl' );
        $html = file_get_html( $url );
        $pagesItem = $html->find( 'div.pages a' );
        foreach ( $pagesItem as $item )
        {
            $lastLink = $item->href;
            $cntPages = $item->plaintext;
        }
        $message = sprintf( 'Last link - %s', $lastLink );
        Zend_Registry::get( 'logger' )->debug( $message );
        $message = sprintf( 'Cnt pages - %s', $cntPages );
        Zend_Registry::get( 'logger' )->debug( $message );
        $links = array();
        for ( $i = 1; $i <= $cntPages; $i ++ )
        {
            $link = change_query( 'p=' . $i, $lastLink );
            $links[] = $this->getHost() . $link;
            $message = sprintf( 'New link - %s', $link );
            Zend_Registry::get( 'logger' )->debug( $message );
        }
        return $links;
    }

    /**
     *
     * Получаем элементы резульата поиска
     * @param unknown_type $linkListPage
     */
    public function getLinksItem( $linkListPage )
    {
        $message = sprintf( 'getLinksItem - %s', $linkListPage );
        Zend_Registry::get( 'logger' )->debug( $message );
        $result = array();
        $html = file_get_html( $linkListPage );
        $items = $html->find( 'div.inmain div.list div.shop_row' );
        foreach ( $items as $item )
        {
            $link = $item->find( 'table.pictable td.pictable_text h5 a', 0 )->href;
            $result[] = $this->getHost() . $link;
            $message = sprintf( 'Link of item - %s', $link );
            Zend_Registry::get( 'logger' )->debug( $message );
        }
        return $result;
    }

    /**
     *
     * Сохраняем страничку и покупаем
     * @param unknown_type $linksItem
     */
    public function grab( $linksItem )
    {
        $result = array();
        foreach ( $linksItem as $link )
        {
            $result[] = $this->saveItem( $link );
            $message = sprintf( 'Saved item - %s', $link );
            Zend_Registry::get( 'logger' )->debug( $message );
        }
        return $result;
    }

    public function saveItem( $url )
    {
        $html = file_get_html( $url );
        $mainBlock = $html->find( 'div.inmain div.shop_row', 0 );
        $title = $mainBlock->find( 'h5 a', 0 )->plaintext;
        $filename = $this->_makeFilename( $title );
        $filenameHtml = $filename . '.html';
        file_put_contents( $filenameHtml, $mainBlock->__toString() );
        $data = array();
        $data['url'] = $url;
        $data[ 'title' ] = iconv( 'utf-8', 'windows-1251', $title );
        $src = $mainBlock->find( 'img.jd_ti', 0 )->getAttribute( 'src' );
        $data[ 'text_img' ] = $this->getHost() . $src;
        $filenameTxt = $filename . '.txt';
        file_put_contents( $filenameTxt, $data );
        return $filenameTxt;
    }

    private function _makeFilename( $title )
    {
        assert( ! empty( $title ) );
        $result = clean_filename( $title );
        $result = trim( $result );
        $result = trim( $result, '.' );
        $result = iconv( 'utf-8', 'windows-1251', $result );
        $result = $this->_makeFullPath( $result );
        return $result;
    }

    private function _makeFullPath( $file )
    {
        $result = $this->getSavePath() . '/' . $file;
        return $result;
    }
}